package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.RadeExcludeStudent;
import com.xmy.cultivate.entity.Student;
import com.xmy.cultivate.entity.views.SelectStudent;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/radeExcludeStudent")
@Scope("prototype")
public class RadeExcludeStudentController extends BaseController {
    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    IStudentService iStudentService;

    @Autowired
    IRadeExcludeStudentService iRadeExcludeStudentService;

    //学生列表
    @GetMapping("getRadeExcludeStudentStudent")
    public Result getRadeExcludeStudentStudent(@RequestParam Map<String, String> reMap) {
        int pageNum = StringUtils.isBlank(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isBlank(reMap.get("pageSize")) ? 10: Integer.parseInt(reMap.get("pageSize"));
        String subjectsId = reMap.get("subjectsId");
        String schoolId = reMap.get("schoolId");
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String yearClassId = reMap.get("yearClassId");
        String name = reMap.get("name");
        Page<SelectStudent> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SelectStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "student_name", name);
//        queryWrapper.ge("residue_course_count", 3);//两次课以上的
        queryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        iRadeExcludeStudentService.getRadeExcludeStudentStudent(page, queryWrapper);
        return new Result(Code.OK, new PageResult<>(page.getRecords(), page.getTotal(), page.getPages()));
    }

    @GetMapping()
    public Result getRadeExcludeStudentList(@RequestParam Map<String, String> reMap) {
        int pageNum = StringUtils.isBlank(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isBlank(reMap.get("pageSize")) ? 10: Integer.parseInt(reMap.get("pageSize"));
        Page<RadeExcludeStudent> page = new Page<>(pageNum, pageSize);
        String subjectsId = reMap.get("subjectsId");
        String schoolId = reMap.get("schoolId");
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String yearClassId = reMap.get("yearClassId");
        String name = reMap.get("name");
        QueryWrapper<RadeExcludeStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*,school_ids AS school_id,subjects_ids AS subjects_id,(SELECT name FROM organization WHERE id = rade_exclude_student.school_ids) AS schoolName");
        queryWrapper.inSql(StringUtils.isNotBlank(name), "student_id", "select id from student where name like '%" + name + "%'");
        queryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_ids", schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_ids", subjectsId);
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByDesc("id");
        iRadeExcludeStudentService.page(page, queryWrapper);
        if (!page.getRecords().isEmpty()) {
            for (RadeExcludeStudent r : page.getRecords()) {
                Student byId = iStudentService.getById(r.getStudentId());
                if (byId != null) {
                    r.setHeadImg(byId.getHeadImg());
                    r.setStudentName(byId.getName());
                }
            }
        }
        return new Result(Code.OK, new PageResult<>(page.getRecords(), page.getTotal(), page.getPages()));
    }

    @PostMapping()
    public Result saveRadeExcludeStudent(@RequestBody RadeExcludeStudent radeExcludeStudent) {
        radeExcludeStudent.setType(1);
        radeExcludeStudent.setClassifyId(1L);
        radeExcludeStudent.setDeleted(0);
        radeExcludeStudent.setSchoolIds(radeExcludeStudent.getSchoolId());
        radeExcludeStudent.setSubjectsIds(radeExcludeStudent.getSubjectsId());
        iRadeExcludeStudentService.save(radeExcludeStudent);
        return new Result(Code.OK, null);
    }

    @DeleteMapping()
    public Result deleteRadeExcludeStudent(@RequestParam Map<String, String> reMap) {
        String id = reMap.get("id");
        if (StringUtils.isBlank(id)) {
            return new Result(Code.ERROR);
        }
        iRadeExcludeStudentService.remove(new QueryWrapper<RadeExcludeStudent>().inSql("id", id));
        return new Result(Code.OK, null);
    }

    @GetMapping("excel")
    public Result getRadeExcludeStudentListExcel(@RequestParam Map<String, String> reMap) {
        int pageNum = StringUtils.isBlank(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isBlank(reMap.get("pageSize")) ? 10: Integer.parseInt(reMap.get("pageSize"));
        Page<RadeExcludeStudent> page = new Page<>(pageNum, pageSize);
        String subjectsId = reMap.get("subjectsId");
        String schoolId = reMap.get("schoolId");
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String yearClassId = reMap.get("yearClassId");
        String name = reMap.get("name");
        QueryWrapper<RadeExcludeStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*,school_ids AS school_id,subjects_ids AS subjects_id,(SELECT name FROM organization WHERE id = rade_exclude_student.school_ids) AS schoolName");
        queryWrapper.inSql(StringUtils.isNotBlank(name), "student_id", "select id from student where name like '%" + name + "%'");
        queryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_ids", schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_ids", subjectsId);
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByDesc("id");
        iRadeExcludeStudentService.page(page, queryWrapper);
        if (!page.getRecords().isEmpty()) {
            for (RadeExcludeStudent r : page.getRecords()) {
                Student byId = iStudentService.getById(r.getStudentId());
                if (byId != null) {
                    r.setHeadImg(byId.getHeadImg());
                    r.setStudentName(byId.getName());
                }
                if (r.getYearClassId() != null && r.getYearClassId() > 0) {
                    String yearClassForNum = ConfigData.getYearClassForNum(r.getYearClassId());
                    r.setYearClassName(yearClassForNum);
                }
                if (r.getSubjectsId() != null && !r.getSubjectsId().isEmpty()) {
                    String[] subjectsIds = r.getSubjectsId().split(",");
                    if (subjectsIds.length == 1) {
                        String subjectsNameForNum = ConfigData.getSubjectsNameForNum(Integer.valueOf(r.getSubjectsId()));
                        r.setSubjectsId(subjectsNameForNum);
                    }
                }
                if (r.getQuarterNum() != null && r.getQuarterNum() > 0) {
                    String quarterForNum = ConfigData.getQuarterForNum(r.getQuarterNum());
                    r.setQuarterNumName(quarterForNum);
                }
            }
        }
        String fileName = "数据管理";

        ExcelUtil.export(fileName, page.getRecords(), RadeExcludeStudent.class, response);

        return new Result(Code.OK,null,"导出成功");
    }
}
